
<template>
  <rm-layout-top splitcolor="white" :splitwidth=5 :fixsize=90 :canDrag=false>
      <template v-slot:fix>
          <div style="width:100%;height:90px;border:1px solid #CCC">
              <table style="width:100%;font-size:12px;">
                  <tr>
                      <td align="right">维护计划</td>
                      <td>
                          <el-input v-model="handexeobj.planname" style="width:220px" disabled></el-input>
                      </td>
                      <td align="right">执行人</td>
                      <td>
                          <rm-ddl-common datatype="userinfo" dataname="用户" v-model="handexeobj.maintuser" width="100px" @change="maintuserChange"></rm-ddl-common>
                      </td>
                      <td align="right">执行时间</td>
                      <td>
                          <el-date-picker v-model="handexeobj.maintdt" type="datetime" placeholder="选择日期时间" style="width:180px" :clearable="false">
                          </el-date-picker>
                      </td>
                      <!-- <td>
                          <el-checkbox v-model="handexeobj.finishflag" true-label="1" false-label="0">是否完成</el-checkbox>
                      </td> -->
                  </tr>
                  <tr>
                      <td align="right" style="vertical-align:top">备注说明</td>
                      <td colspan="5">
                          <el-input v-model="handexeobj.description" type="textarea"></el-input>
                      </td>
                  </tr>
              </table>
          </div>
      </template>
      <rm-layout-bottom splitcolor="white" :splitwidth="showbtn?5:0" :canDrag=false :fixsize="showbtn?40:0">
          <template v-slot:fix>
              <div style="width:100%;margin-top:10px" v-if="showbtn">
                   <div style="float:left" v-if="devmaintobj.devmaintid">启动时间为：{{devmaintobj.createdt}}</div>
                  <div style="float:right">
                    
                      延时
                      <el-input v-model="txtdelaydays" style="width:30px;text-align:right"></el-input>
                      <el-select v-model="txtdelaytype" placeholder="请选择"  style="width:65px">
                                  <el-option key="0" label="日" value="0"></el-option>
                                  <el-option key="1" label="小时" value="1"></el-option>
                                  <el-option key="2" label="分钟" value="2"></el-option>
                      </el-select>后
                      <el-button @click="btnRemindVal" style="margin-right:30px">启动</el-button>
                      <el-button @click="btnDefaultVal">默认值</el-button>
                      <el-button @click="btnConfirm" icon="el-icon-document">确定</el-button>
                      <el-button @click="btnCancel" icon="el-icon-close">取消</el-button>
                  </div>
              </div>
          </template>
          <rm-layout-right splitcolor="white" movingcolor="#0000ff" :splitwidth=5 :fixsize=200>
              <template v-slot:fix>
                  <div style="height:100%">
                      <vxe-table :data.sync="rightData" :select-config="selectConfig" :sort-config="{trigger: 'cell'}" :edit-config="{trigger: 'click',mode: 'cell',showStatus: true,showIcon: false,}" border fit height="auto" header-align="center" highlight-current-row highlight-hover-row highlight-hover-column ref="rxTable" resizable size="mini" stripe show-header-overflow="tooltip" show-overflow="tooltip" @cell-dblclick="dblclickCell">
                          <vxe-table-column field="comexeresult" title="常见执行结果(双击选择)" width="170" sortable align="center" v-if="selrow.resulttype!='3'" key="1">
                          </vxe-table-column>
                          <vxe-table-column field="comexeresult" title="常见执行结果" width="170" sortable align="center" v-else key="2">
                              <template v-slot="{row}">
                                  <el-upload ref="upload" action="#" accept="*" :on-change="fileChange">
                                      <el-button icon="el-icon-upload2">上传</el-button>
                                  </el-upload>
                              </template>
                          </vxe-table-column>
                      </vxe-table>
                  </div>
              </template>
              <div style="height:100%;width:100%;">
                  <vxe-table :data.sync="leftData" :select-config="selectConfig" :sort-config="{trigger: 'cell'}" :edit-config="{trigger: 'click',mode: 'cell',showStatus: true,showIcon: false,}" border fit height="auto" header-align="center" highlight-current-row highlight-hover-row highlight-hover-column ref="lxTable" resizable size="mini" show-header-overflow="tooltip" show-overflow="tooltip" @current-change="CurrentChangeEvent" :cell-class-name="cellclassname" class="mytable-style">
                      <vxe-table-column field="maintcontent" title="执行内容" width="250" sortable></vxe-table-column>
                      <vxe-table-column field="maintresult" title="执行结果" width="120" sortable :edit-render="{name:'input', autoselect: true,events:{change:(row,val) => {row.row.maintresult = val.target.value}}}">
                      </vxe-table-column>
                      <vxe-table-column field="historyresult" title="最近三次历史执行结果" width="140" sortable></vxe-table-column>
                      <vxe-table-column   field="fileoperate" title="选择附件" width="70" align="center" sortable>
                          <template v-slot="{row}">
                              <el-button @click="fileclick(row)"  v-has="'PADD'"  icon="el-icon-folder-opened"></el-button>
                          </template>
                      </vxe-table-column>
                      <vxe-table-column field="fileid" title="附件ID" width="70" align="center" sortable></vxe-table-column>
                      <vxe-table-column field="filename" title="附件名称" width="120" sortable >
                          <template v-slot="{row}">
                                  <el-link style="font-size: 12px;"  @click="openFile(row.fileid, row.filename)">{{row.filename}}</el-link>
                          </template>
                      </vxe-table-column>
                  </vxe-table>
              </div>
          </rm-layout-right>
           <div class="upload-button" hidden>
                  <el-upload ref="upload" :action="url" accept="*" :http-request="uploadOpt">
                      <el-button size="small" plain>选择文件</el-button> 
                  </el-upload>
              </div>
      </rm-layout-bottom>
  </rm-layout-top>
</template>

<script type="text/javascript">
import XE from "xe-utils"; //引入工具类
import axios from "axios";
export default {
  props: {
      formobj: { type: Object },
      tableobj: { type: Array },
      showbtn: { type: Boolean, default: true },
      devmainobj:{ type: Object },
      devmaintobj:{type:Object},
  },
  data() {
      return {
          handexeobj: {},
          rightData: [],
          leftData: [],
          selectConfig: { tigger: "row" },
          selrow: {},
          txtdelaydays:"3",
          txtdelaytype:"0",
          hdevmaintobj:"",
          devdocpath: "",
          url: this.$tool.baseUrl.replace(/api/, "z") + "fileupload",
          rxTableRow:{},
          ismakeup:"",//是否补上一下记录 0 否 1 是
      };
  },
  updated() {},
  created(){
      const s = document.createElement("script");
      s.type = "text/javascript";
      s.src = this.$tool.baseUrl.replace(/api/, "PageOffice") + "pageoffice.js";
      document.body.appendChild(s);
      if (!window.polframe02) {
          window.polframe02 = document.createElement("iframe");
          polframe02.src = PO_code + "parent.htm";
          polframe02.style.display = "none";
          polframe02.id = "poiframe1";
          document.body.appendChild(polframe02);
          window.poModalDlg = {
              dialogPob: null,
              init: function () {
                  this.dialogPob = document.createElement("div");
                  this.dialogPob.className = "pobmodal-overlay";
                  this.dialogPob.style.display = "none";
                  this.dialogPob.innerHTML = '<div id="pobmodal-dialog"><h2>提示</h2>当前文档正处于打开状态，请点击<a style=\'color:#ff0000;\' href="javascript:POBrowser.resumePO();" > 这里 </a>切换PageOffice窗口继续查看或编辑文档。<div class="button-holder"></br></br><a class="button blue" href="javascript:POBrowser.resumePO();" > 立即切换 PageOffice 窗口</a></div></div>';
                  document.getElementsByTagName("body")[0].appendChild(this.dialogPob);
              },
              showPobDlg: function () {
                  this.dialogPob.style.display = "block";
              },
              hidePobDlg: function () {
                  this.dialogPob.style.display = "none";
              },
          };
          poModalDlg.init();
      }
  },
  mounted() {
      this.handexeobj = XE.clone(this.formobj, true);
      this.hdevmaintobj = XE.clone(this.devmaintobj, true);
      this.leftData = this.tableobj;
      if (this.leftData.length > 0) {
          let row = this.leftData[0];
          this.$refs.lxTable.setCurrentRow(row);
          this.$refs.lxTable.setActiveCell(row, "maintresult");
          this.initRightData(row);
      }
     this. hdevmaintid = this.devmaintid;
  },
  methods: {
      //默认值
      btnDefaultVal() {
          this.leftData.forEach((item) => {
              if (item.resulttype == "1") {
                  item.maintresult = "√";
              } else if (item.resulttype == "2") {
                  item.maintresult = item.commonresults.split("|@|")[0];
              }
          });
          this.$refs.lxTable.updateListenter();
      }, //确定
      btnConfirm() {      
          this.$emit("change", this.handexeobj, this.leftData);
          let rowindex = this.leftData.findIndex((item) => !item.maintresult);
          if (rowindex > -1) {
              let realrowindex = rowindex + 1;
              this.$alert("第" + realrowindex + "行必须填写执行结果！", "提示").then(() => {
                  let row = this.leftData[rowindex];
                  this.$refs.lxTable.setCurrentRow(row);
                  this.$refs.lxTable.setActiveCell(row, "maintresult");
                  this.initRightData(row);
              });
              return false;
          }
          let data = this.leftData.filter((item) => !!item.maintresult);
          if (data.length > 0) {
              var curDate = new Date();
              var nextDate = new Date(this.devmainobj.nextruntime);
              if(nextDate < curDate && XE.toDateString(curDate, "yyyy-MM-dd ") != XE.toDateString(nextDate, "yyyy-MM-dd ")){

                  this.$confirm("当前任务应该执行日期为：" + XE.toDateString(nextDate, "yyyy-MM-dd ") + "(已超期)，您是否要补最近一次执行？", "提示", { confirmButtonText: "确定", cancelButtonText: "取消", type: "warning" })
                  .then(() => {
                     this.ismakeup = "1";
                     this.exeplansave();
                  })
                  .catch(() => {
                    this.ismakeup = "0";
                    this.exeplansave();
                  });
              } 
              else{
                  this.ismakeup = "0";
                  this.exeplansave();
              }
          } else {
              this.$alert("当前计划没有填写任何执行结果，无法保存！", "提示");
          }
      }, //取消
      exeplansave(){
          this.$tool
                      .request({
                          url: "ucent/devalert",
                          method: "post",
                          data: {
                              action: "exesave",
                              form: this.handexeobj,
                              table: this.leftData,
                              devmaintid:this.hdevmaintobj.devmaintid,
                              makeupflag :this.ismakeup,
                          },
                      })
                      .then((res) => {
                          if (res.errcode == "1") {
                              this.$pub.message(this, "保存成功！", "success");
                              this.$emit("change", "success", res.plandata);
                          } else {
                              this.$alert(res.errmsg, "保存失败");
                          }
                      });
      },

      btnCancel() {
          this.$emit("change", "close");
      }, //换切换
      CurrentChangeEvent({ row, column }) {
          this.initRightData(row);
      },
      initRightData(row) {
          this.rightData = [];
          this.selrow = row;
          if (row.resulttype == "1") {
              this.rightData.push({ comexeresult: "√", resulttype: "1" });
              this.rightData.push({ comexeresult: "X", resulttype: "1" });
          } else if (row.resulttype == "2") {
              let maintresult = row.commonresults.split("|@|");
              if (maintresult.length > 0) {
                  maintresult.forEach((item) => {
                      if (!!item) {
                          this.rightData.push({ comexeresult: item, resulttype: "2" });
                      }
                  });
              }
          } else {
              this.rightData.push({ comexeresult: "上传", resulttype: "3" });
          }
      },
      //右边表格双击选择
      dblclickCell({ row, column }) {
          this.selrow.maintresult = row.comexeresult;
          this.$refs.lxTable.updateListenter();
      },
      //fileupload文件上传,这里暂时取名称，后面看文件写入何处(待做)
      fileChange(file, fileList) {
          if (!!file) {
              this.selrow.maintresult = file.name;
              this.$refs.lxTable.updateListenter();
          }
      },
      maintuserChange(code, name) {
          this.$rm_Login({
              type: "请输入工号和密码",
              userid: code,
              username: name,
              changeflag: true,
              btn: {
                  confirmBtnText: "确定",
                  cancelBtnText: "取消",
              },
          })
              .then((res) => {
                  if (res.flag == "1") {
                      this.handexeobj.maintuser = code;
                  } else {
                      //登录失败
                  }
              })
              .catch((e) => {
                  this.handexeobj.maintuser = this.formobj.maintuser;
              });
      },
      cellclassname({ row, column }) {
          if (column.property == "maintcontent") {
              return "col-gray";
          }
      },
      // xx天后启动提醒
      btnRemindVal(){
              this.$tool
                  .request({
                      url: "ucent/devalert",
                      method: "post",
                      data: {
                          action: "delaydaysave",
                          delayday:this.txtdelaydays,
                          delaytype:this.txtdelaytype,
                          form: this.handexeobj,
                          devmaintid:this.hdevmaintobj.devmaintid,
                      },
                  })
                  .then((res) => {
                      if (res.errcode == "1") {
                          this.$pub.message(this, "启动成功！", "success");
                          this.$emit("change", "success", res.plandata);
                      } else {
                          this.$alert(res.errmsg, "启动失败");
                      }
                  });
      },
       fileclick(row) {
          this.rxTableRow = row;
          this.$refs["upload"].$refs["upload-inner"].handleClick();
          // if (this.devdocpath != "") {
          //     this.$refs["upload"].$refs["upload-inner"].handleClick();
          // } else {
          //     this.$alert("请先设置设备文档上传目录", "提示");
          // }
      },
      uploadOpt(content) {
          let formData = new FormData();
          formData.append("action", "FileUploadComm");
          formData.append("sysno", "DEV");
          formData.append("pfileclassname", "设备维护保养记录");
          formData.append("fileclasscode", "devmaint_record");
          formData.append("fileclassname", "设备维护保养记录");
          formData.append("recordid", this.selrow.employeeno);
          formData.append("file", content.file);
          formData.append("currentuser", this.$tool.getSession(this.$tool.loginuserno));
          formData.append("hospcode", this.$tool.getSession(this.$tool.selectedhis));
          axios
              .post(this.url, formData, { "Content-Type": "multipart/form-data", "Content-Type": "application/msword" })
              .then((res) => {
                  if (res.data.flag == "1") {
                      this.$message.success("上传成功！");
                      this.rxTableRow.filename = content.file.name;
                      this.rxTableRow.fileid = res.data.msg;
                  } else {
                      this.$alert(res.data.msg, "提示");
                  }
              })
              .catch((e) => {
                  this.$alert(e, "提示");
              });
      },
       openFile(id, filename) {
          let subauth ="1"; //this.detailData.SubmitAuth ? "1" : "0";
          let downauth = "1";//this.detailData.DownLoadAuth ? "1" : "0";
          let printauth = "1";//this.detailData.PrintAuth ? "1" : "0";
          var nalist = filename.split(".");
          let name = nalist[nalist.length - 1].toString().toLowerCase();
          if (name == "pdf") {
              let url = this.$tool.baseUrl.replace(/api/, "PageOffice") + "pdf?id=" + id.toString() + "%26downauth=" + downauth + "%26printauth=" + printauth;
              POBrowser.openWindowModeless(url, "width=1150px;height=900px;");
          } else if (name == "jpg" || name == "jpeg" || name == "png") {
              let url = this.$tool.baseUrl.replace(/api/, "PageOffice") + "image?id=" + id.toString() + "%26printauth=" + printauth;
              POBrowser.openWindowModeless(url, "width=1150px;height=900px;");
          } else {
              let url = this.$tool.baseUrl.replace(/api/, "PageOffice") + "word?id=" + id.toString() + "%26subauth=" + subauth;
              POBrowser.openWindowModeless(url, "width=1150px;height=900px;");
          }
      },
  },
};
</Script>

<style scoped>
table {
  font-size: 12px;
  border-collapse: separate;
}
table tr td {
  padding: 2px 3px 2px 3px;
}
/deep/.el-textarea__inner {
  display: block;
  resize: vertical;
  padding: 2px 4px;
}
/deep/ .integratedclass {
  width: 100%;
}
/deep/ .halfclass {
  width: 50%;
}
/deep/ tr.vxe-body--row.row--current {
  color: #000000;
  font-weight: bold;
}
/deep/ .el-input.is-disabled .el-input__inner {
  background-color: #f5f7fa;
  border-color: #e4e7ed;
  color: #0808de;
  cursor: not-allowed;
}
/deep/ .mytable-style .vxe-body--column.col-gray {
  background-color: rgb(248, 248, 248);
  color: rgb(8, 8, 8);
}
</style>